package com.ruoyi.system.xcenter.endpoints;

import cn.hutool.json.JSONUtil;
import com.ruoyi.common.json.JSON;
import com.ruoyi.common.json.JSONObject;
import com.ruoyi.common.utils.ExceptionUtil;
import com.ruoyi.system.domain.DataxXchangeLog;
import com.ruoyi.system.mapper.DataxXchangeLogMapper;
import com.ruoyi.system.xcenter.EPContext;
import com.ruoyi.system.xcenter.IEndPoint;
import com.ruoyi.system.xcenter.PassiveEndPointAbstract;
import com.ruoyi.system.xcenter.exception.DataXException;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
public class XiwoEndPoint extends PassiveEndPointAbstract implements IEndPoint {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private DataxXchangeLogMapper dataxXchangeLogMapper;
    @Autowired
    private HttpServletRequest httpServletRequest;

    @Override
    public Object dataUpload(EPContext context, Object body, Integer type) {
        // TODO 捷顺数据上行
        return null;
    }

    @PostMapping(value = {"/xiwo/{function}"})
    @ResponseBody
    @Override
    public Object dataDownload( EPContext context,Object request, Integer type)  {
        String uri = httpServletRequest.getRequestURI();
        JSONObject result = new JSONObject();
        DataxXchangeLog xchangeLog = new DataxXchangeLog();
        xchangeLog.setEndGroupId(2l);
        try {
            String body = IOUtils.toString(httpServletRequest.getReader());
            JSONObject unmarshal = JSON.unmarshal(body, JSONObject.class);
            unmarshal.remove("inCarPic");
            unmarshal.remove("inSmallPic");
            unmarshal.remove("outSmallPic");
            unmarshal.remove("outCarPic");
            body = JSONUtil.toJsonStr(unmarshal);

            switch (uri){
                case "/xiwo/space":
                    // 车位数对接、心跳转换
                    logger.info("xiwo api post {} push body : {}",uri,body);
                    this.consumeEndPoint(body, 1);
                    break;
                case "/xiwo/inPark":
                    // 入场对接
                    logger.info("xiwo api post {} push body ",uri);
                    this.consumeEndPoint(body, 2);
                    break;
                case "/xiwo/outPark":
                    // 入场对接
                    logger.info("xiwo api post {} push body ",uri);
                    this.consumeEndPoint(body, 3);
                    break;
            }
        }catch (Exception e){
            if(e instanceof DataXException){
                //
            }else {
                xchangeLog.setErrorLog(uri+" 解析异常:"+ ExceptionUtil.getExceptionMessage(e));
                logger.info("jieshun api post error : {}",e);
            }
            dataxXchangeLogMapper.insertDataxXchangeLog(xchangeLog);
        }

        result.set("code",0);
        result.set("msg","请求成功");

        return result;
    }



}
